// pages/imgListPage/ingListPage.js
import { circle } from '../../server/circle';
import { file } from '../../server/file';
import { server } from '../../config/server';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        server_addr: server.server_addr,
        circle: {
            commentCount: 0,
            content: "",
            id: 0,
            isLike: 0,
            likeCount: 0,
            photoId: 0,
            time: "",
            title: "",
            uid: 0,
            imgUrl: ''
        },
        comment: '',
        commentList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        let circleId = options.circle;
        this.loadCircleInfo(circleId);
        this.getComment(circleId);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    returnMainPage() {
        wx.navigateBack({ delta: 1 });
    },

    loadCircleInfo(circleId) {
        circle.getCircleInfo(circleId).then(async (res) => {
            let imgUrl = await file.downloadPublicFile(res.data.photoId);
            imgUrl = imgUrl.data.url;
            res.data.imgUrl = imgUrl;
            this.setData({ circle: res.data });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网路',
                icon: 'none',
                duration: 1000
            });
        });
    },
    // 输入评论内容
    inputComment(e) {
        this.setData({
            comment: e.detail.value
        });
    },
    // 发送评论
    comment() {
        let circleId = this.data.circle.id;
        let comment = this.data.comment;
        circle.comment(circleId, comment).then((res) => {
            if (res.success) {
                wx.showToast({
                    title: '评论成功',
                    icon: 'none',
                    duration: 500
                });
                // 删除输入框里的内容
                this.setData({ comment: '' });
                // 刷新评论区
                this.getComment(circleId);
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 500
                })
            }
        }).catch((err) => {
            wx.showToast({
                title: '请检查网路',
                icon: 'none',
                duration: 1000
            });
        });
    },
    // 刷新评论区
    getComment(circleId) {
        circle.getComment(circleId).then((res) => {
            this.setData({ commentList: res.data });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网路',
                icon: 'none',
                duration: 1000
            });
        });
    },
    // 点赞圈子
    likeCircle() {
        let circleId = this.data.circle.id;
        let islike = this.data.circle.isLike;
        if (islike) {
            circle.nolikeCircle(circleId).then((res) => {
                if (res.success) {
                    let circle = this.data.circle;
                    circle.isLike = false;
                    circle.likeCount--;
                    this.setData({ circle });
                }
            }).catch((err) => {
                wx.showToast({
                    title: '请检查网路',
                    icon: 'none',
                    duration: 1000
                });
            });
        } else {
            circle.likeCircle(circleId).then((res) => {
                if (res.success) {
                    let circle = this.data.circle;
                    circle.isLike = true;
                    circle.likeCount++;
                    this.setData({ circle });
                }
            }).catch((err) => {
                wx.showToast({
                    title: '请检查网路',
                    icon: 'none',
                    duration: 1000
                });
            });
        }
    }
})